package com.zjz.Filter;

import com.zjz.pojo.User;
import com.zjz.util.Constants;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class SysFilter implements Filter {
    public void init(FilterConfig filterConfig) throws ServletException {
    }

    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse)servletResponse;
        //从session中获取用户
        User user = (User) request.getSession().getAttribute(Constants.USER_SESSION);
        if(user==null){//说明已经注销了
            response.sendRedirect("/error.jsp");
        }else {
            filterChain.doFilter(servletRequest,servletResponse);
        }
    }

    public void destroy() {
    }
}
